<?php
namespace Home\Controller;
use Think\Controller;
class RegisterController extends Controller {
    
    public function index(){
		
		$this->display('Register/register');
		
	}
	
	public function register(){
		
		$data = I('post.');
		//设置自动验证规则 模型类文件中
		//设置自动完成规则 模型类文件中
		$model = D('User');

		//自动创建数据
		$resArr = $model->create($data); 

		if($resArr === false || empty($resArr)){
			$errMsg = $model->getError();
			$this->error($errMsg ? $errMsg : '未知错误');
		}
		
		if($model->add()){
			$userdata = $model->where(array('username' => $data['username']))->find();
			$id = $userdata['id'];
			
			$_SESSION['username'] = $data['username'];
			$_SESSION['id'] = $id;
			$_SESSION['type'] = 0;
			$this->success('添加成功!', U('Home/Index/index'),1);
		}else{
			$this->error($model->getError());
		}
	}
}